/*******************************************************************************
 * Copyright (c) 2010, 2017 西安秦晔信息科技有限公司
 * Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 *******************************************************************************/
package com.qinyeit.serviceapp.service.system.impl;

import com.qinyeit.serviceapp.entity.system.SystemLog;
import com.qinyeit.serviceapp.repository.system.SystemLogRepository;
import com.qinyeit.serviceapp.service.system.SystemLogService;
import com.qinyetech.springstage.core.date.DateTime;
import com.qinyetech.springstage.core.date.DateUtils;
import com.qinyetech.springstage.core.lang.StringUtils;
import com.qinyetech.springstage.core.service.BaseService;
import com.qinyetech.springstage.core.web.util.ByteFormatUtils;
import eu.bitwalker.useragentutils.UserAgent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Calendar;

/**
 * <p>ClassName: com.qinyetech.webapp.service.system.impl.SystemLogServiceImpl
 * <p>Function: 系统日志
 * <p>date: 2018-03-14 19:43
 *
 * @author wuqing
 * @version 1.0
 * @since JDK 1.8
 */
@Service
public class SystemLogServiceImpl extends BaseService<SystemLog, Long> implements SystemLogService {
    @Autowired
    private SystemLogRepository systemLogRepository;

    @Override
    public void save(UserAgent userAgent,
                     String ip,
                     String requestUri,
                     Long time,
                     String operation,
                     String currentAccount,
                     String methodName) {

        SystemLog sysLog = new SystemLog();
        //执行时间
        sysLog.setRuntime(time);
        //账号
        if (StringUtils.isNotBlank(currentAccount)) {
            sysLog.setManagementAccount(currentAccount);
        } else {
            sysLog.setManagementAccount("未登陆用户");
        }
        sysLog.setOperation(operation);
        sysLog.setMethod(methodName);
        // 设置IP地址
        //sysLog.setIp(HttpContextUtils.getIpAddress(request));
        sysLog.setIp(ip);
        //sysLog.setRequestUri(StringUtils.abbr(request.getRequestURI(), 255));
        sysLog.setRequestUri(requestUri);
        //设置操作系统
        sysLog.setOsName(userAgent.getOperatingSystem().getName());
        sysLog.setBrowserName(userAgent.getBrowser().getName());
        String memory = "最大内存:%s,已分配内存:%s,已分配内存中的剩余空间:%s,最大可用内存:%s";
        Long maxMemory = Runtime.getRuntime().maxMemory();
        Long totalMemory = Runtime.getRuntime().totalMemory();
        Long freeMemory = Runtime.getRuntime().freeMemory();

        sysLog.setMemory(String.format(memory,
                ByteFormatUtils.formatByteSize(maxMemory),
                ByteFormatUtils.formatByteSize(totalMemory),
                ByteFormatUtils.formatByteSize(freeMemory),
                ByteFormatUtils.formatByteSize(maxMemory - totalMemory + freeMemory)
        ));
        systemLogRepository.save(sysLog);
    }

    @Transactional
    @Override
    public void deleteLogs(int offsetMonth) {
        Calendar calendar=Calendar.getInstance();
        calendar.set(Calendar.DAY_OF_MONTH,1); //当月1日
        //开始时间
       DateTime curDateTime= DateUtils.beginOfDay(Calendar.getInstance());
       DateTime dateTime=DateUtils.offsiteMonth(curDateTime,-offsetMonth);
        systemLogRepository.deleteBefore(dateTime);
    }
}
